//
//  GaoButtonIl.m
//  DuTe
//
//  Created by Gikki Ares on 2020/12/8.
//  Copyright © 2020 vgemv. All rights reserved.
//

#import "JsBarrageButton.h"
#import "JsGeometryManager.h"
#import "JsBundleManager.h"

@interface JsBarrageButton()

@end


@implementation JsBarrageButton

#pragma mark LifeCycle
//initWighFrame时,就确保传入正确的UI数据.
- (instancetype)initWithFrame:(CGRect)frame vm:(JsBarrageButtonVm *)vm {
	self = [super initWithFrame:frame];
	if (self) {
		self.vm = vm;
		//设置vm的selfSize,或者根据vm的size来设置frame;
		[self commonInit];
	}
	return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
	if(self == [super initWithCoder:coder]) {
		
	}
	return self;
}


- (void)awakeFromNib {
	[super awakeFromNib];
	self.vm = [JsBarrageButtonVm new];
	[self commonInit];
}

- (void)commonInit {
	//初始化View中不变的参数.
	CGFloat selfHeight = _vm.wrapperContentSize.height;
	self.layer.cornerRadius = selfHeight/2;
	self.layer.masksToBounds = YES;
	self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
	self.layer.borderWidth = 1;
	self.layer.borderColor = [UIColor grayColor].CGColor;

	mbtn_toggle = [[UIButton alloc]init];
	[mbtn_toggle addTarget:self  action:@selector(onClickToggleButton:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:mbtn_toggle];
	
	mbtn_send = [[UIButton alloc]init];
	[mbtn_send addTarget:self  action:@selector(onClickSendButton:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:mbtn_send];
	
	mtf = [UITextField new];
	[self addSubview:mtf];
	mtf.userInteractionEnabled = NO;
	mtf.clipsToBounds = YES;
	mtf.font = self.vm.mfont_titleLabel;
	mtf.textColor = self.vm.mcolor_titleLabel;
	mtf.textAlignment = self.vm.mTextAlignment;
	
	miv = [[UIImageView alloc] init];
	[self addSubview:miv];
	
}


//自己的frame还是交给父视图去控制.
//layoutSubview只负责直接子视图的布局.
//layoutSubviews的不改变本身的frame;
- (void)layoutSubviews {
	[super layoutSubviews];
//	NSLog(@"%s,state is %d",__FUNCTION__,state);
	[self updateUiWithVm:_vm];
}

//updateUi可以更新frame;
- (void)updateUi {
	
	[self setNeedsLayout];
}

- (void)updateUiWithVm:(JsBarrageButtonVm *) vm {
	[UIView animateWithDuration:0.2 animations:^{
		JsBarrageButtonVmStyle * style = [vm currentVmStyle];
		self.frame = [JsGeometryManager frameWithNewWidth:style.size.width originFrame:self.frame];
		self->miv.frame = style.imageViewFrame;
		self->miv.image = [JsBundleManager iconInBundleNamed:style.imageName];
		self->miv.userInteractionEnabled = NO;
		self->mtf.frame = style.labelFrame;
		self->mtf.text = style.mstr_title;
//		self->mtf.font = style.mfont_titleLabel;
		self->mbtn_toggle.frame = style.toggleButtonFrame;
		self->mbtn_send.frame = style.sendButtonFrame;
		self->mbtn_toggle.backgroundColor = style.mcolor_toggleButton;
	}];
	
}
								
#pragma mark 2 BasicFunction

#pragma mark 3 Interface
	//设置背景图片
//
//- (UIFont *)textFont {
//	return self.vm.mfont_titleLabel;
//}
//
//
//- (void)setTitle:(NSString *)title {
//	[self setTitle:title withFont:self.vm.mfont_titleLabel];
//}
//
//- (void)setTitle:(NSString *)title withFont:(UIFont *)font {
//	[self setTitle:title withFont:font textColor:self.vm.mcolor_titleLabel];
//}
//
//- (void)setTitle:(NSString *)title withFont:(UIFont *)font textColor:(UIColor *)textColor {
//	if(!mtf) {
//		mtf = [UILabel new];
//		[self addSubview:mtf];
//	}
//	mtf.text = title;
//	mtf.font = font;
//	mtf.textColor = textColor;
//
//	self.vm.mstr_title = title;
//	self.vm.mfont_titleLabel = font;
//	self.vm.mcolor_titleLabel = textColor;
//	self.vm.mastr_title = nil;
//}

#pragma mark 3 Interface
- (void)setState:(int)state {
	self.vm.state = state;
	[self setNeedsLayout];
}

- (int)state {
	return self.vm.state;
}



//- (void)setImage:(UIImage *)image {
//	[self setSelectedImage:image unselectedImage:image];
//}
//
////设置图片则显示imageView;
//- (void)setSelectedImage:(UIImage *)selectedImage unselectedImage:(UIImage *)unselectedImage {
//	if(!miv) {
//		miv = [UIImageView new];
//		[self insertSubview:miv atIndex:0];
//	}
//	mImage_selected = selectedImage;
//	mImage_unselected = unselectedImage;
//	miv.hidden = NO;
//	//默认设置未选择
//	[self setUnselected];
//}
//
//- (void)setUnselected {
//	isSelected = false;
//	miv.image = mImage_unselected;
//}

//- (void)setSelected {
//	isSelected = true;
//	miv.image = mImage_selected;
//}
//
//- (void)toggle {
//	if(isSelected) {
//		[self setUnselected];
//	}
//	else {
//		[self setSelected];
//	}
//}
//
//- (void)updateUi {
//	miv.frame = [self.vm imageViewFrame];
//	mtf.frame = [self.vm labelFrame];
//	mtf.textColor = self.vm.mcolor_titleLabel;
//	mtf.font = self.vm.mfont_titleLabel;
//	mtf.textAlignment = self.vm.mTextAlignment;
//	if(self.vm.mastr_title) {
//		mtf.attributedText = self.vm.mastr_title;
//	}
//	else {
//		mtf.text = self.vm.mstr_title;
//	}
//
//	mbtn.frame = self.bounds;
//}
//
//- (void)updateFontColor:(UIColor *)color {
//	self.vm.mcolor_titleLabel = color;
//	[self updateUi];
//}
//
//- (void)updateAttributedText:(NSAttributedString *)astr {
//	self.vm.mastr_title = astr;
//	[self updateUi];
//}

#pragma mark 2 interface
//- (GaoButtonLiiVm *)vm {
//	return _vm;
//}
//
//- (GaoButtonLiiVm *)selectedVm {
//	return _selectedVm;
//}
//- (void)updateLabelAlignment:(NSTextAlignment)textAlignment {
//	self.vm.mTextAlignment = textAlignment;
//	[self updateUi];
//}
//



#pragma mark 5 Event
- (IBAction)onClickToggleButton:(id)sender {
	if(self.state == 0) {
		self.state = 1;
	}
	else {
		self.state = 0;
	}
	if(_delegate && [_delegate respondsToSelector:@selector(jsBarrageButton:stateChanged:)]) {
		[_delegate jsBarrageButton:self stateChanged:self.state];
	}
	//frame设置为一样的,不会触发layout;
	[self setNeedsLayout];
}

- (IBAction)onClickSendButton:(id)sender {
	if(self.state == 1){
		self.state = 2;
	}
	else {
		self.state = 1;
	}
	if(_delegate && [_delegate respondsToSelector:@selector(jsBarrageButton:stateChanged:)]) {
		[_delegate jsBarrageButton:self stateChanged:self.state];
	}
	[self setNeedsLayout];
}


@end


